package projectServlet;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOException;
import dao.EmployeeDAOFactory;
import dao.mysql.EmployeeDAOFactoryImpl;
import dto.UserDTO;

/**
 * Servlet implementation class Admin
 */
@WebServlet("/Admin")
public class Admin extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Admin()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	protected void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		dosubProcess(request, response);
		RequestDispatcher rd = request.getRequestDispatcher("AdminEditor.jsp");
		rd.forward(request, response);
	}

	protected void dosubProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{

		String SearchButton = request.getParameter("Search");
		String AddButton = request.getParameter("Add");
		String DeleteButton = request.getParameter("Delete");
		String EditButton = request.getParameter("Edit");

		EmployeeDAOFactory empFactory = new EmployeeDAOFactoryImpl();
		UserDTO user = new UserDTO();

		if (SearchButton != null && SearchButton.equals("Search"))
		{
			String SUserID = request.getParameter("UserID");
			int UserID = Integer.parseInt(SUserID.trim());

			try
			{
				user = empFactory.getEmployeeDAO().findUser(UserID);
				request.setAttribute("Users", user);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (AddButton != null && AddButton.equals("Add"))
		{

			String UserName = (String) request.getParameter("UserName");
			String SUserAge = (String) request.getParameter("UserAge");
			String SSupervisorID = (String) request
					.getParameter("SupervisorID");
			if (!ValidateInvliadInput(SUserAge)
					|| !ValidateInvliadInput(SSupervisorID)
					|| !ValidateInvalidName(UserName))
				{
				return;
				}
			int UserAge = Integer.parseInt(SUserAge.trim());
			int SupervisorID = Integer.parseInt(SSupervisorID.trim());

			String StaffType = (String) request.getParameter("StafType");
			String StaffRole = (String) request.getParameter("StaffRole");
			String Gender = (String) request.getParameter("Gender");
			String SPassword = (String) request.getParameter("Password");
			user.set_age(UserAge);
			user.set_gender(Gender);
			user.set_name(UserName);
			user.set_password(SPassword);
			user.set_supID(SupervisorID);
			user.set_staffType(StaffRole);
			user.set_domain(StaffType);
			user.set_medicalLeaveEntitlement(60);
			user.set_medicalLeave(0);
			user.set_overTimeDays(0);
			user.set_compensationLeave(0);
			if (StaffRole.equals("pro"))
			{
				user.set_annualLeaveEntitlement(18);
				user.set_annualLeave(18);
			}
			else if (StaffRole.equals("admin"))
				{
				user.set_annualLeave(14);
				user.set_annualLeaveEntitlement(14);
				}

			try
			{
				empFactory.getEmployeeDAO().insertUser(user);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (DeleteButton != null && DeleteButton.equals("Delete"))
		{
			String SUserID = (String) request.getParameter("UserID");
			if (SUserID == null || SUserID.trim().equals(""))
				return;

			int UserID = Integer.parseInt(SUserID);
			user.set_userID(UserID);

			try
			{
				empFactory.getEmployeeDAO().deleteUser(user);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (EditButton != null && EditButton.equals("Edit"))
		{
			String UserName = (String) request.getParameter("UserName");
			String SUserAge = (String) request.getParameter("UserAge");
			String SSupervisorID = (String) request
					.getParameter("SupervisorID");
			if (!ValidateInvliadInput(SUserAge)
					|| !ValidateInvliadInput(SSupervisorID)
					|| !ValidateInvalidName(UserName))
				return;

			int UserAge = Integer.parseInt(SUserAge.trim());
			int SupervisorID = Integer.parseInt(SSupervisorID.trim());

			String StaffType = (String) request.getParameter("StafType");
			String StaffRole = (String) request.getParameter("StaffRole");
			String Gender = (String) request.getParameter("Gender");
			String SPassword = (String) request.getParameter("Password");
			String SUserID = (String) request.getParameter("UserID");
			if (SUserID == null || SUserID.trim().equals(""))
				return;

			int UserID = Integer.parseInt(SUserID);

			user.set_userID(UserID);
			user.set_age(UserAge);
			user.set_gender(Gender);
			user.set_name(UserName);
			user.set_password(SPassword);
			user.set_supID(SupervisorID);
			user.set_staffType(StaffRole);
			user.set_domain(StaffType);
//			user.set_medicalLeaveEntitlement(60);
			if (StaffRole.equals("pro"))
				user.set_annualLeaveEntitlement(18);
			else if (StaffRole.equals("admin"))
				user.set_annualLeaveEntitlement(14);

			try
			{
				empFactory.getEmployeeDAO().updateUserByAdmin(user);
			}
			catch (DAOException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private boolean ValidateInvliadInput(String inputS)
	{

		for (int i = 0; i < inputS.length(); i++)
		{
			if (inputS.charAt(i) < '0' || inputS.charAt(i) > '9')
				return false;
		}

		return true;

	}

	private boolean ValidateInvalidName(String inputS)
	{
		int spaceCount = 0;
		for (int i = 0; i < inputS.length(); i++)
		{
			if ((inputS.charAt(i) < 'a' || inputS.charAt(i) > 'z')
					&& (inputS.charAt(i) < 'A' || inputS.charAt(i) > 'Z') )
			{
				if(inputS.charAt(i) == ' ')
				{
					spaceCount++;
					if(spaceCount > 4)
						return false;
				}
			}
				//return false;
		}
		return true;
	}
}
